<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %><%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2024/12/25
  Time: 11:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<%--<%--%>
<%--    // 获取参数--%>
<%--    String TIEZIID = request.getParameter("TIEZIID");--%>
<%--    String USERID = request.getParameter("USERID");--%>

<%--    // 数据库连接信息--%>
<%--    String url = "jdbc:mysql://localhost:3306/db1";--%>
<%--    String user = "root";--%>
<%--    String password = "123456";--%>

<%--    try {--%>
<%--        // 加载驱动--%>
<%--        Class.forName("com.mysql.jdbc.Driver");--%>

<%--        // 建立连接--%>
<%--        Connection conn = DriverManager.getConnection(url, user, password);--%>
<%--        System.out.println("conn = " + conn);--%>

<%--        // 创建SQL语句--%>
<%--        String sql = null;--%>
<%--        String CheckLiker = null;--%>
<%--        int CheckLike = 0;--%>
<%--        String CheckStarr = null;--%>
<%--        int CheckStar = 0;--%>
<%--        String CheckCoinr = null;--%>
<%--        int CheckCoin = 0;--%>
<%--        PreparedStatement pstmt = null;--%>
<%--        if (request.getParameter("CheckLike") != null){--%>
<%--            CheckLiker = request.getParameter("CheckLike");--%>
<%--            CheckLike = CheckLiker.equals("true") ? 1 : 0;--%>
<%--            sql = "INSERT INTO tiezi_status (TIEZIID,USERID,DIANZAN) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE DIANZAN = VALUES(DIANZAN)";--%>
<%--            pstmt = conn.prepareStatement(sql);--%>
<%--            pstmt.setInt(3, CheckLike);--%>
<%--        }--%>
<%--        if (request.getParameter("CheckStar") != null){--%>
<%--            CheckStarr = request.getParameter("CheckStar");--%>
<%--            CheckStar = CheckStarr.equals("true") ? 1 : 0;--%>
<%--            sql = "INSERT INTO tiezi_status (TIEZIID,USERID,SHOUCANG) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE SHOUCANG = VALUES(SHOUCANG)";--%>
<%--            pstmt = conn.prepareStatement(sql);--%>
<%--            pstmt.setInt(3, CheckStar);--%>
<%--        }--%>
<%--        if (request.getParameter("CheckCoin") != null){--%>
<%--            CheckCoinr = request.getParameter("CheckCoin");--%>
<%--            CheckCoin = CheckCoinr.equals("true") ? 1 : 0;--%>
<%--            sql = "INSERT INTO tiezi_status (TIEZIID,USERID,TOUBI) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE TOUBI = VALUES(TOUBI)";--%>
<%--            pstmt = conn.prepareStatement(sql);--%>
<%--            pstmt.setInt(3, CheckCoin);--%>
<%--        }--%>

<%--        pstmt.setString(1, TIEZIID);--%>
<%--        pstmt.setString(2, USERID);--%>

<%--        // 执行SQL语句--%>
<%--        pstmt.executeUpdate();--%>
<%--        // 关闭连接--%>
<%--        pstmt.close();--%>
<%--        conn.close();--%>

<%--        out.println("内容已成功保存到数据库");--%>
<%--    } catch (Exception e) {--%>
<%--        e.printStackTrace();--%>
<%--        out.println("保存内容失败: " + e.getMessage());--%>
<%--    }--%>
<%--%>--%>
<%
    // 获取参数
    String TIEZIID = request.getParameter("TIEZIID");
    String USERID = request.getParameter("USERID");
    String CheckLiker = request.getParameter("CheckLike");
    String CheckStarr = request.getParameter("CheckStar");
    String CheckCoinr = request.getParameter("CheckCoin");

    if (!USERID.equals("NULL")){
        // 数据库连接信息
        String url = "jdbc:mysql://localhost:3306/db1";
        String user = "root";
        String password = "123456";

        int CheckLike = "true".equals(CheckLiker) ? 1 : 0;
//        System.out.println("CheckLike = " + CheckLike);
        int CheckStar = "true".equals(CheckStarr) ? 1 : 0;
//        System.out.println("CheckStar = " + CheckStar);
        int CheckCoin = "true".equals(CheckCoinr) ? 1 : 0;
//        System.out.println("CheckCoin = " + CheckCoin);

        try {
            // 加载驱动
            Class.forName("com.mysql.jdbc.Driver");

            // 建立连接
            try (Connection conn = DriverManager.getConnection(url, user, password)) {

                // 创建SQL语句
                String sql = "INSERT INTO tiezi_status (TIEZIID,USERID,DIANZAN,SHOUCANG,TOUBI) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE DIANZAN = VALUES(DIANZAN), SHOUCANG = VALUES(SHOUCANG), TOUBI = VALUES(TOUBI)";
                try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                    pstmt.setString(1, TIEZIID);
//                    System.out.println("TIEZIID = " + TIEZIID);
                    pstmt.setString(2, USERID);
//                    System.out.println("USERID = " + USERID);
                    pstmt.setInt(3, CheckLike);
                    pstmt.setInt(4, CheckStar);
                    pstmt.setInt(5, CheckCoin);

//                    System.out.println("成功！");
                    // 执行SQL语句
                    pstmt.executeUpdate();
                    out.println("内容已成功保存到数据库");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            out.println("保存内容失败: " + e.getMessage());
        }
    }

%>
<body>

</body>
</html>
